﻿#include <qg/qg.h>

int main(void)
{
	k_mesg("TEXTURE CREATE TEST\n");

	qgDevice* dev = qgDevice::New(NULL, 640, 480, QGSDF_MSAA | QGSDF_LAYOUT);

	if (dev)
	{
		qgStub* stub = dev->GetStub();
		qgFreeType* font = qgFreeType::New();

		dev->SetBackground(tpl::ColorValue(0xFF604040));

		//
		qgTexture* utx = dev->CreateTexture(NULL, 256, 256, 0, 0, 0, QGCF8_L);

		qgTextureMap tm;
		if (utx->EnterMap(&tm, 0))
		{
			memset(tm.data, 0xFF, tm.pitch*tm.height);

			kbyte* d = (kbyte*)tm.data;
			for (int y = 0; y < tm.height; y++)
			{
				d[y + y*tm.pitch] = 0x00;
				d[(tm.width - y) + y*tm.pitch] = 0x00;
			}

			utx->LeaveMap();
		}

		//
		float x = 0.0f, y = 0.0f;
		bool ok = false;

		while (dev->Run())
		{
			float spd = 200.0f;
			float adv = stub->GetAdvance();
			const tpl::Point& size = stub->GetSize();

			if (stub->IsKeyDown(QIK_UP))
				y -= spd * adv;
			if (stub->IsKeyDown(QIK_DOWN))
				y += spd * adv;
			if (stub->IsKeyDown(QIK_LEFT))
				x -= spd * adv;
			if (stub->IsKeyDown(QIK_RIGHT))
				x += spd * adv;

			x = k_clamp(x, 0.0f, (float)(size.x - 250));
			y = k_clamp(y, 0.0f, (float)(size.y - 250));

			dev->Enter();

			dev->DrawRect(tpl::Rect((int)x, (int)y, (int)x + 250, (int)y + 250), tpl::ColorValue(0xFFFFFFFF), utx);

			font->WriteFormat(5, 5, 0, "[%.2f]", stub->GetRunFps());

			dev->Leave();
			dev->Flush();
		}

		kobj_unload(utx);
		font->Unload();
		dev->Unload();
	}

	return 0;
}
